
using B.S.BasicData.Api.Read.Filters;
using B.S.BasicData.Infrastructure;
using B.S.BasicData.Infrastructure.Implement;
using B.S.BasicData.Infrastructure.Interfaces;
using Microsoft.EntityFrameworkCore;
using NLog.Web;
using System.Reflection;
using Yitter.IdGenerator;

using Microsoft.OpenApi.Models;

namespace B.S.BasicData.Api.Read
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.

       
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
          
            //1注册上下文 
            builder.Services.AddDbContext<SCDbContext>(x => x.UseSqlServer(builder.Configuration.GetConnectionString("DB")));
            //2.日志
            builder.Host.UseNLog();
            //3.映射
            builder.Services.AddAutoMapper(typeof(AutoMapperProfile));
            //4.中介者
            builder.Services.AddMediatR(cfg=>cfg.RegisterServicesFromAssemblies(Assembly.GetExecutingAssembly()));
            //5.仓储
            builder.Services.AddScoped(typeof(IBaseRepository<>),typeof(BaseRepository<>));
            //6.雪花Id
            YitIdHelper.SetIdGenerator(new IdGeneratorOptions(0));
            //7.注册异常过滤器到控制器的Filters里
            builder.Services.AddControllers(c=>c.Filters.Add<MyExceptionFilter>());
        

            // 配置Swagger
            builder.Services.AddSwaggerGen(o =>
            {
                //中文注释
                o.IncludeXmlComments(AppContext.BaseDirectory + "/B.S.BasicData.Api.Read.xml", true);
            });

            var app = builder.Build();


            app.UseCors(o=>o.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
            // Configure the HTTP request pipeline.
            //if (app.Environment.IsDevelopment())
           // {
                app.UseSwagger();
                app.UseSwaggerUI();
            //}
            app.UseAuthorization();

            app.UseHttpsRedirection();

            //app.UseAuthorization();


            app.MapControllers();

            app.Run();
        }
    }
}
